package com.china.osourcechina;

import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.china.adapter.EmojiAdapter;
import com.china.adapter.TweetDetailAdapter;
import com.china.base.BaseActivity;
import com.china.utils.AcquisitionTime;
import com.china.view.MyLinearLayout;
import com.china.vo.Tweet_user;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/12.
 */
public class TweetDetailActivity extends BaseActivity {
    private ImageView iv_tweet_detail_emoji;
    private LinearLayout ll_buttom;
    private boolean check = true;
    private LinearLayout myliner;
    private List<String> list = new ArrayList<>();
    private List<Integer> emoji = new ArrayList<>();
    private List<Integer> details = new ArrayList<Integer>();
    private List<String> strings = new ArrayList<String>();
    private View view;
    private GridView gv_tweet_emoji_list;
    private EditText et_tweet_detail_text;
    private Button bt_tweet_detail_send;
    private ListView lv_tweet_detail_list;
    private int hight = 188;
    private TextView tv_tweet_detail_uname;
    private TextView tv_tweet_detail_time;
    private TextView tv_tweet_detail_body;
    private ImageView iv_tweet_detail_img;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private MyLinearLayout tweet_detail_myline;
    private TextView tv_tweet_detail_comment;
    private int width = 0;


    @Override
    protected int loadWindowLayout() {
        return R.layout.activity_tweet_detail;
    }

    @Override
    protected void initTitleBar() {

    }

    @Override
    protected void initViews() {
        iv_tweet_detail_img = (ImageView)findViewById(R.id.iv_tweet_detail_img);
        tv_tweet_detail_uname = (TextView)findViewById(R.id.tv_tweet_detail_uname);
        tv_tweet_detail_time = (TextView)findViewById(R.id.tv_tweet_detail_time);
        tv_tweet_detail_body = (TextView)findViewById(R.id.tv_tweet_detail_body);
        iv_tweet_detail_emoji = (ImageView)findViewById(R.id.iv_tweet_detail_emoji);
        et_tweet_detail_text = (EditText)findViewById(R.id.et_tweet_detail_text);
        bt_tweet_detail_send = (Button)findViewById(R.id.bt_tweet_detail_send);
        lv_tweet_detail_list = (ListView)findViewById(R.id.lv_tweet_detail_list);
        tv_tweet_detail_comment = (TextView)findViewById(R.id.tv_tweet_detail_comment);
        tweet_detail_myline = (MyLinearLayout)findViewById(R.id.tweet_detail_myline);
        ll_buttom = (LinearLayout)findViewById(R.id.ll_buttom);
        myliner = (LinearLayout)findViewById(R.id.mylist);
        List<Tweet_user> tweet_users = (List<Tweet_user>) getIntent().getSerializableExtra("list");
//        TextView textVieww = new TextView(this);
//        textVieww.setText("");
//        tweet_detail_myline.addView(textVieww);
        if(tweet_users!=null) {
            if (tweet_users.size() != 0) {
                for (int i = 0; i < tweet_users.size(); i++) {
                    width = tweet_users.get(i).getName().length() * 2;
                    TextView textView = new TextView(this);
                    textView.setText(tweet_users.get(i).getName() + "、");
                    textView.setTextColor(getResources().getColor(R.color.Indigo_colorPrimary));
                    textView.setTextSize(12);
                    tweet_detail_myline.setmCellWidth(100);
                    tweet_detail_myline.setmCellHeight(25);
                    tweet_detail_myline.addView(textView);
                }
            }
        }
        if(getIntent().getStringExtra("url")==null){
            iv_tweet_detail_img.setImageResource(R.mipmap.widget_dface);
        }else {
            imageLoader.displayImage(getIntent().getStringExtra("url"),iv_tweet_detail_img);
        }
        tv_tweet_detail_comment.setText(getIntent().getStringExtra("count"));
        tv_tweet_detail_uname.setText(getIntent().getStringExtra("uname"));
        tv_tweet_detail_time.setText(AcquisitionTime.getStandardDate(AcquisitionTime.dateToTimestamp(getIntent().getStringExtra("time"))));
        tv_tweet_detail_body.setText(getIntent().getStringExtra("body"));
        view = LayoutInflater.from(TweetDetailActivity.this).inflate(R.layout.activity_tweet_emoji,null);
        iv_tweet_detail_emoji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(check){
                    myliner.setVisibility(View.VISIBLE);
                    iv_tweet_detail_emoji.setImageResource(R.mipmap.btn_emoji_pressed);
                    myliner.addView(view);
                    ll_buttom.setPadding(ll_buttom.getPaddingLeft(),ll_buttom.getPaddingTop(),ll_buttom.getPaddingRight(),ll_buttom.getPaddingBottom()+hight);
                    hight = myliner.getHeight();
                    check = false;
                }else {
                    hight = myliner.getHeight();
                    iv_tweet_detail_emoji.setImageResource(R.mipmap.btn_emoji);
                    myliner.setVisibility(View.GONE);
                    myliner.removeView(view);
                    ll_buttom.setPadding(ll_buttom.getPaddingLeft(),ll_buttom.getPaddingTop(),ll_buttom.getPaddingRight(),ll_buttom.getPaddingBottom()-hight);
                    check = true;
                }
            }
        });
        initData();
    }

    private void initData() {
        gv_tweet_emoji_list = (GridView)view.findViewById(R.id.gv_tweet_emoji_list);
        for(int i = 0;i<104;i++){
            int id = getResources().getIdentifier("smiley_"+i,"mipmap",this.getPackageName());
            emoji.add(id);
        }
        gv_tweet_emoji_list.setAdapter(new EmojiAdapter(emoji,TweetDetailActivity.this));
        gv_tweet_emoji_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String str = "smiley_"+position;
                SpannableString ssb = new SpannableString(str);
                //根据图片id生成drawble
                Drawable drawable = TweetDetailActivity.this.getResources().getDrawable(emoji.get(position));
                drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
                ImageSpan span = new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE);
                ssb.setSpan(span,0,str.length(), Spanned.SPAN_COMPOSING);
                et_tweet_detail_text.append(ssb);
                details.add(emoji.get(position));
            }
        });
        bt_tweet_detail_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                strings.add(et_tweet_detail_text.getText().toString());
                et_tweet_detail_text.setText("");
                lv_tweet_detail_list.setAdapter(new TweetDetailAdapter(strings,TweetDetailActivity.this));
//                lv_tweet_detail_list.setAdapter(new ArrayAdapter<String>(TweetDetailActivity.this, R.layout.activity_tweet_detail_item,R.id.tv_tweet_detail_item_emoji,strings));
            }
        });
    }
}
